package com.bilibili.juc.base;

import java.util.concurrent.*;

//演示线程池三种常用分类
public class ThreadPoolDemo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //固定大小线程池
        ExecutorService threadPool1 = Executors.newFixedThreadPool(5); //5个窗口

        //单线程线程池
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor(); //一个窗口

        //创建一个缓存线程池，它会根据需要创建新的线程，如果线程空闲时间超过60秒，就会被终止并从池中移除。
        ExecutorService threadPool3 = Executors.newCachedThreadPool();

        //10个顾客请求
        try {
            for (int i = 1; i <=10; i++) {
                //执行
                threadPool3.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" 办理业务");
                });
            }
            //submit和excute的区别就是它需要返回值
            Future<String> a = threadPool3.submit(()-> {
                return "测试返回值！";
            });
            String result = a.get();
            System.out.println(result);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭
            threadPool3.shutdown();
        }


    }

}
